package com.eascs.o2o.recruit.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.o2o.recruit.entity.ApplyForInfoVO;
import com.eascs.o2o.recruit.entity.OfficeForInfoVO;
import com.eascs.o2o.recruit.service.ApplyForInfoService;

@Controller
public class ApplyForInfoController{
	@Autowired
	@Qualifier("applyForInfoServiceImpl")
	private ApplyForInfoService applyForInfoService;
	
	@Reference

	/**
	 * 进入借款首页
	 * @return
	 */
	@RequestMapping("/index")
	public String queryRecruitInfo( HttpSession session) {
		session.setAttribute("formFlag", "true");
		return "/recruit/indexInfo";
	}
	/**
	 * 进入借贷人信息填写页面
	 */
	@RequestMapping("/applyInfo")
	public String queryRecruitApplyInfo( HttpSession session) {
		session.setAttribute("formFlag", "true");
		return "/recruit/applyInfo";
	}
	
	/**
	 * 进入借贷人店面填写页面
	 */
	@RequestMapping(value = "/officeInfo")
	public ModelAndView queryRecruitStoreInfo(@ModelAttribute ApplyForInfoVO applyForInfoVO, HttpSession session) {
		
		ModelAndView mv = new ModelAndView();
		if (applyForInfoVO.getIdCard() != null) {
			applyForInfoVO.setCreateTime(new Date());
			session.setAttribute("applyForInfoVO", applyForInfoVO);
			mv.setViewName("/recruit/officeInfo");
		} else {
			mv.setViewName("/recruit/indexInfo");
		}
		return mv;
	}
	/**
	 * 保存借贷人信息，进入借款成功页面
	 */
	@RequestMapping(value = "/saveApply")
	public String queryRecruitSaveApply(@ModelAttribute OfficeForInfoVO officeForInfoVO, HttpSession session) {
		String formFlag = (String) session.getAttribute("formFlag");//防止表单重复提交标记
		if (formFlag == null) {
			formFlag = "true";
		}
		if ("true".equals(formFlag)) {
			ApplyForInfoVO applyForInfoVO = (ApplyForInfoVO) session.getAttribute("applyForInfoVO");
			applyForInfoService.saveApplyForInfo(applyForInfoVO);
			if (applyForInfoVO.getId() != null) {
				officeForInfoVO.setApplyerId(applyForInfoVO.getId());
				applyForInfoService.saveOfficeForInfo(officeForInfoVO);
			}
			session.setAttribute("applyForInfoVO", null);
		}		
		session.setAttribute("formFlag", "false");
		return "/recruit/applySuccess";
	}
//	/**
//	 * 查询相应区域负责人信息
//	 * @param applyForInfoVO
//	 * @return
//	 */
//	@RequestMapping(value = "/applayforinfo/save", method = RequestMethod.POST)
//	public ModelAndView queryRecruitInfo(@ModelAttribute ApplyForInfoVO applyForInfoVO) {
//		ModelAndView mv = new ModelAndView();
//		applyForInfoVO.setCreateTime(new Date());
//		applyForInfoService.saveApplyForInfo(applyForInfoVO);
//		LoanLinkman loanLinkman = applyForInfoService.queryLoanLinkman(applyForInfoVO);
//		if (loanLinkman == null) {
//			applyForInfoVO.setProvince(OTHERS);
//			loanLinkman = applyForInfoService.queryLoanLinkman(applyForInfoVO);
//		}
//		mv.addObject("lead", loanLinkman);
//		mv.setViewName("/recruit/subApply");
//		return mv;
//	}

}
